extends Node

var players=[]
var enemys=[]
onready var playerView=$PlayerField/playerView
onready var enemyView=$enemyField/enemyView
onready var cam=$Cam


var donghuaPlayer


func _ready():
	randomize()
	
	Global.Battle=self
	var player
	player=load("res://Model/Chess.gd").new()
	Map.addM2V(player,$PlayerField/playerView)
	players.append(player)
	player=load("res://Model/Chess.gd").new()
	Map.addM2V(player,$PlayerField2/playerView)
	players.append(player)
	player=load("res://Model/Chess.gd").new()
	Map.addM2V(player,$PlayerField3/playerView)
	players.append(player)
	var enemy
	enemy=load("res://Model/Chess.gd").new()
	Map.addM2V(enemy,$enemyField/enemyView)
	enemys.append(enemy)
	enemy=load("res://Model/Chess.gd").new()
	Map.addM2V(enemy,$enemyField2/enemyView)
	enemys.append(enemy)
	enemy=load("res://Model/Chess.gd").new()
	Map.addM2V(enemy,$enemyField3/enemyView)
	enemys.append(enemy)
	
	var donghua=[0]
	battle(donghua)
	donghuaPlayer=GDLab.DongHuaPlayer.new()
	donghuaPlayer.play(donghua)
	
	
	
	
	
	
	pass
func battle(donghua):
	for player in players:
		print("player Attack")
		var enemy=getRandomEnemy()
		if randf()<0.3:
			#donghua.append(Action.attack(player,enemy).donghua)
			donghua.append(Action.doubleAttack(player,enemy).donghua)
		else:
			donghua.append(Action.attack(player,enemy).donghua)
			#donghua.append(Action.doubleAttack(player,enemy).donghua)
		if checkWin():
			return
	for enemy in enemys:
		print("enemy Attack")
		var player=getRandomPlayer()
		if randf()<0.3:
			#donghua.append(Action.attack(enemy,player).donghua)
			donghua.append(Action.doubleAttack(enemy,player).donghua)
		else:
			donghua.append(Action.attack(enemy,player).donghua)
			#donghua.append(Action.doubleAttack(enemy,player).donghua)
		if checkWin():
			return
	battle(donghua)
func checkWin():
	var result=false
	if players.size()==0:
		print("敌人胜利")
		result=true
	if enemys.size()==0:
		print("玩家胜利")
		result=true
	return result
func getRandomPlayer():
	return players[randi()%players.size()]
func getRandomEnemy():
	return enemys[randi()%enemys.size()]
func removeChess(chess):
	if players.has(chess):
		players.erase(chess)
	if enemys.has(chess):
		enemys.erase(chess)
